//
//  RiskBaseData.js
//  SugLisSystem
//  初始化险种信息
//  Created by Mr.Right on 2013-04-20.
//  Copyright 2013 Mr.Right. All rights reserved.
//  最幸福的事情就是看着自己喜欢的人慢慢入睡，❤~
//
var tGlobalInput = require('GlobalInput');
var dB = require('untils/DB');
var exeSQL = require('untils/ExeSQL');
var pubFun = require('untils/PubFun')
var riskFilter = require('suggest/RiskFilter');

function RiskBaseData(mainRiskCode, contNo, mainPolNo, appntNo, insuredNo) {
	this.mainRiskCode = mainRiskCode;
	this.contNo = contNo;
	this.mainPolNo = mainPolNo;
	this.appntNo = appntNo;
	this.insuredNo = insuredNo;
	this.baseRiskData = {};
}

RiskBaseData.prototype.init = function() {
	//初始化投被保人信息
	//投被保人信息
	var appntSchema = {};
	var appntDB = new dB('LSAppnt');
	appntDB['contno'] = this.contNo;
	if (appntDB.getInfo()) {
		appntSchema = appntDB.getSchema();
		if (appntSchema['age'] == null) {
			appntSchema['appntage'] = pubFun.calInterval(appntSchema['appntbirthday'], pubFun.getCurrentDate(), 'Y');
		}
	}

	var insuSchema = {};
	var insuDB = new dB('LSInsured');
	insuDB['insuredno'] = this.insuredNo;
	insuDB['contno'] = this.contNo;
	if (insuDB.getInfo()) {
		insuSchema = insuDB.getSchema();
		if (insuSchema['age'] == null) {
			insuSchema['age'] = pubFun.calInterval(insuSchema['birthday'], pubFun.getCurrentDate(), 'Y');
		}
	}
	//初始化主险信息
	var mainRiskData = initRiskData(this.mainRiskCode, this.contNo, this.mainPolNo, this.insuredNo, insuSchema, this.appntNo, appntSchema);
	this.baseRiskData[this.mainRiskCode] = mainRiskData;
	//筛选附加险
	var subRiskCodeArr = riskFilter.getSubRiskList(tGlobalInput, this.mainRiskCode, '-1', this.contNo, this.insuredNo);
	for (var i = 0; i < subRiskCodeArr.length; i++) {
		var tempSubRiskCode = subRiskCodeArr[i]['riskcode'];
		var tempSubRiskData = initRiskData(tempSubRiskCode, this.contNo, this.mainPolNo, this.insuredNo, insuSchema, this.appntNo, appntSchema);
		this.baseRiskData[tempSubRiskCode] = tempSubRiskData;
	}
}

RiskBaseData.prototype.getBaseRiskData = function() {
	return this.baseRiskData;
}
/**
 * 初始化单个险种信息
 * @param riskCode 险种代码
 * @param contNo 建议书号
 * @param mainPolNo 主保单号
 * @param insuredNo 被保人号
 * @param insuSchema 被保人信息
 * @param appntNo 投保人号
 * @param appntSchema 投保人信息
 * */
function initRiskData(riskCode, contNo, mainPolNo, insuredNo, insuSchema, appntNo, appntSchema) {
	//初始化主险信息
	var riskName = '';
	var subRiskFlag = 'M';
	var riskApp = new dB('LMRiskApp');
	riskApp['riskcode'] = riskCode;
	if (riskApp.getInfo()) {
		riskName = riskApp['riskname'];
		subRiskFlag = riskApp['subriskflag'];
	}
	//险种界面
	var riskScreenSQL = "SELECT * FROM LMRISKSCREEN WHERE RISKCODE = '" + riskCode + "' ORDER BY CTRLINDEX";
	var screenSet = exeSQL.execSQL(riskScreenSQL, exeSQL.TYPE_OBJECT);
	//险种信息
	var riskPolDB = new dB('LSPol');
	riskPolDB['riskcode'] = riskCode;
	riskPolDB['mainpolno'] = mainPolNo;
	riskPolDB['insuredno'] = insuredNo;
	var riskPolSet = riskPolDB.query();
	var riskPolSchema = {};

	var riskEnable = false;
	var riskModify = false;

	var polNo = Ti.Platform.createUUID();
	//主险时POLNO和MAINPOLNO一致
	if (subRiskFlag == 'M') {
		polNo = mainPolNo;
	}
	//从POL表中载入险种信息
	if (riskPolSet.length > 0) {
		riskEnable = true;
		riskModify = true;
		riskPolSchema = riskPolSet[0];
		riskPolNo = riskPolSchema['polno'];
	}

	//险种信息组合
	var riskScreenBaseItem = {
		contNo : contNo,
		subRiskFlag : subRiskFlag,
		riskCode : riskCode,
		riskName : riskName,
		mainPolNo : mainPolNo,
		polNo : polNo,
		appntNo : appntNo,
		appntSchema : appntSchema,
		insuNo : insuredNo,
		insuSchema : insuSchema,
		screenSet : screenSet,
		riskData : riskPolSchema,
		enable : riskEnable,
		modify : riskModify
	}

	return riskScreenBaseItem;
}

module.exports = RiskBaseData;